﻿using Grundy.Message;

namespace Grundy.CryptographicServices
{
    public static class CryptographicExtensionMethods
    {
        private static ContainerValidator _containerValidator = new ContainerValidator();

        public static T VerifyPayload<T>(this Container c, CryptographicServicesProvider ems, CryptographicServicesProvider server, CryptographicServicesProvider client)
        {   
            var result = _containerValidator.Validate(c);
            if(!result.IsValid)
                return default(T);

            if (c.Signee == ContainerSigneeType.EntitlementServer)
                return ems.Verify<T>(c);
            if (c.Signee == ContainerSigneeType.Server)
                return server.Verify<T>(c);
            if (c.Signee == ContainerSigneeType.Client)
                return client.Verify<T>(c);
            return default(T);
        }
    }
}